package com.sise.netty.server端和client端通信案例.demo2;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import lombok.extern.slf4j.Slf4j;

/**
 * @author idea
 * @data 2019/4/6
 */
@Slf4j
public class Server {

    public static void main(String[] args) {
        ServerBootstrap serverBootstrap=new ServerBootstrap();
        NioEventLoopGroup boos=new NioEventLoopGroup();
        NioEventLoopGroup worker=new NioEventLoopGroup();
        serverBootstrap
                .group(boos,worker)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {

                        System.out.println("初始化通道！");
                        ch.pipeline().addLast(new StringDecoder());
                        ch.pipeline().addLast(new SimpleChannelInboundHandler<String>() {
                            @Override
                            protected void channelRead0(ChannelHandlerContext ctx, String msg) {
                                System.out.println("接受到数据：");
                                System.out.println(msg);
                            }
                        });
                    }

                })
                .bind(8000);
    }

}
